package com.bosssoft.cloud.wechat.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bosssoft.cloud.user.dto.RegisterDTO;
import com.bosssoft.cloud.user.service.UserCenterService;
import com.bosssoft.cloud.user.vo.LoginedUserVO;
import com.bosssoft.cloud.user.vo.RegisterVO;
import com.bosssoft.cloud.wechat.common.bean.user.UserInfoDTO;
import com.bosssoft.cloud.wechat.common.service.IUserService;
import com.bosssoft.cloud.wechat.core.api.WxOAuth2Service;
import com.bosssoft.cloud.wechat.core.bean.oauth2.WxOAuth2AccessToken;
import com.bosssoft.cloud.wechat.infrastructure.enums.ErrEnum;
import com.bosssoft.cloud.wechat.infrastructure.exception.TPSException;
import com.bosssoft.cloud.wechat.infrastructure.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户账户控制器
 *
 * @author wcy create on 2021/6/29 17:05 下午
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserCenterService userCenterService;
    @Autowired
    private WxOAuth2Service wxOAuth2Service;

    /**
     * 微信通过手机号注册（绑定手机号）
     *
     * @param phoneNumber 手机号
     * @param openid      微信openid
     * @return 用户信息
     */
    @Override
    public UserInfoDTO registByPhone(String phoneNumber, String openid) {
        RegisterDTO registerDTO = new RegisterDTO();
        registerDTO.setPhone(phoneNumber);
        registerDTO.setPassword(phoneNumber);   //密码默认为手机号
        registerDTO.setUserName(phoneNumber);
        registerDTO.setOpenId(openid);
        RegisterVO registerVO;
        try {
            registerVO = userCenterService.registUnified(registerDTO);
        } catch (Exception e) {
            throw new TPSException(ErrEnum.C0110, e.getMessage());
        }
        return BeanUtil.toBean(registerVO, UserInfoDTO.class);
    }

    /**
     * 通过wxCode查看用户是否存在
     *
     * @param wxCode wxCode
     */
    @Override
    public LoginedUserVO getUserBywxCode(String wxCode) {
        // 获取oauth2网页授权token
        WxOAuth2AccessToken token = wxOAuth2Service.getAccessToken(wxCode);
        String openid = token.getOpenId();
        // 通过openId 查询用户信息
        LoginedUserVO userVO = userCenterService.queryUserInfobyOpenId(openid);
        if (userVO == null) {
            log.warn("根据openid:\"{}\"获取用户信息为null", openid);
            userVO = new LoginedUserVO();
            userVO.setOpenId(openid);
        }
        return userVO;
    }

    /**
     * 解绑手机号（注销）
     *
     * @param phoneNumber 手机号
     * @param openid      微信openid
     */
    @Override
    public void untyingPhone(String phoneNumber, String openid) {
        // 通过openid查询用户信息
        LoginedUserVO userVO = userCenterService.queryUserInfobyOpenId(openid);
        // 判断用户是否存在
        Assert.notNull(userVO, ErrEnum.A0201, "用户信息异常，该用户可能未注册");
        String phone = userVO.getPhone();
        // 用户手机号核对
        Assert.isTrue(phoneNumber.equals(phone), ErrEnum.A0403, phone + ":请录入正确的手机号");
        // 执行注销
        Assert.isTrue(userCenterService.unbundPhone(phone) > 0, ErrEnum.C0115, "注销失败");
    }
}
